added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBBrowserHelperObject / Readme.txt
blob54d40c69215878cb50241a095686eaa10035e502
1 ================================================================================
2        Windows APPLICATION: VBBrowserHelperObject Overview                        
3 ===============================================================================
4 /////////////////////////////////////////////////////////////////////////////
5 Summary:
6 The sample demonstrates how to create and deploy a Browser Helper Object. A Browser
7 Helper Object runs within Internet Explorer and offers additional services,  and 
8 the BHO in this sample is used to disable the context menu in IE.
10 To add a BHO to IE, this class should be registered to COM with a class ID (CLSID,
11 {C42D40F0-BEBF-418D-8EA1-18D99AC2AB17} in this sample), and add a key under 
12 "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects".
14 NOTE: 
15 1. On 64bit machine, 32bit IE will use 
16    "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects"
18 2. On Windows Server 2008 or Windows Server 2008 R2, the Internet Explorer Enhanced 
19    Security Configuration (IE ESC) is set to On by default, which means that this BHO
20    can not be loaded by IE. You have to turn off IE ESC for you. 
23 /////////////////////////////////////////////////////////////////////////////
24 Setup and Removal:
26 --------------------------------------
27 In the Development Environment
29 A. Setup
31 For 32bit IE on x86 or x64 OS, run 'Visual Studio Command Prompt (2010)' in the 
32 Microsoft Visual Studio 2010 \ Visual Studio Tools menu as administrator. For 64bit
33 IE on x64 OS, run Visual Studio x64 Win64 Command Prompt (2010).
35 Navigate to the folder that contains the build result VBBrowserHelperObject.dll and 
36 enter the command:
38     Regasm.exe VBBrowserHelperObject.dll /codebase
40 The BHO is registered successfully if the command prints:
41     "Types registered successfully"
43 B. Removal
45 For 32bit IE on x86 or x64 OS, run 'Visual Studio Command Prompt (2010)' in the 
46 Microsoft Visual Studio 2010 \ Visual Studio Tools menu as administrator. For 64bit
47 IE on x64 OS, run Visual Studio x64 Win64 Command Prompt (2010).
49 Navigate to the folder that contains the build result VBBrowserHelperObject.dll and 
50 enter the command:
52     Regasm.exe VBBrowserHelperObject.dll /unregister
54 The BHO is unregistered successfully if the command prints:
56     "Types un-registered successfully"
58 --------------------------------------
59 In the Deployment Environment
61 A. Setup
63 For 32bit IE on x86 or x64 OS, install VBBrowserHelperObjectSetup(x86).msi, the output
64 of the VBBrowserHelperObjectSetup(x86) setup project.
66 For 64bit IE on x64 OS, install VBBrowserHelperObjectSetup(x64).msi outputted by the 
67 VBBrowserHelperObjectSetup(x64) setup project.
69 B. Removal
71 For 32bit IE on x86 or x64 OS, uninstall VBBrowserHelperObjectSetup(x86).msi, the 
72 output of the VBBrowserHelperObjectSetup(x86) setup project. 
74 For 64bit IE on x64 OS, uninstall VBBrowserHelperObjectSetup(x64).msi, the output of
75 the VBBrowserHelperObjectSetup(x64) setup project.
79 ////////////////////////////////////////////////////////////////////////////////
80 Demo:
82 Step1. Open this project in VS2010 and set the platform of the solution to x86. Make
83        sure that the projects VBBrowserHelperObject and VBBrowserHelperObjectSetup(x86)
84            are selected to build in Configuration Manager.
86            NOTE: If you want to use this BHO in 64bit IE, set the platform to x64 and 
87                  select VBBrowserHelperObject and VBBrowserHelperObjectSetup(x64) to build.
88         
89 Step2. Build the solution.
91 Step3. Right click the project VBBrowserHelperObjectSetup(x86) in Solution Explorer, 
92        and choose "Install".
94            After the installation, open 32bit IE and click Tools=>Manage Add-ons, in the 
95            Manage Add-ons dialog, you can find the item 
96            "VBBrowserHelperObject.BHOIEContextMenu" and make sure it is enabled. You may 
97            have to restart IE to make it take effect. 
99            NOTE: You can also use the Regasm command in the "Setup and Removal" section.
101 Step4. Open IE and visit http://www.microsoft.com/. After the page was loaded 
102        completely, right click this page and you will find that the context menu is 
103            disabled.
106 /////////////////////////////////////////////////////////////////////////////
107 Implementation:
109 A. Create the project and add references
111 In Visual Studio 2010, create a Visual Basic / Windows / Class Library project 
112 named "VBBrowserHelperObject". 
114 Right click the project in Solution Explorer and choose "Add Reference". Add
115 "Microsoft HTML Object Library" and "Microsoft Internet Controls" in COM tab.
116 -----------------------------------------------------------------------------
118 B. Implement a basic Component Object Model (COM) DLL
120 A Browser Helper Object is a COM object implemented as a DLL. Making a basic 
121 .NET COM component is very straightforward. You just need to define a 
122 'public' class with ComVisible(true), use the Guid attribute to specify its 
123 CLSID, and explicitly implements certain COM interfaces. For example, 
125 <ComVisible(True), ClassInterface(ClassInterfaceType.None),
126 Guid("C42D40F0-BEBF-418D-8EA1-18D99AC2AB17")>
127 Public Class SimpleObject
128     Implements ISimpleObject
129         ...
130 EndClass
133 You even do not need to implement IUnknown and class factory by yourself 
134 because .NET Framework handles them for you.
136 -----------------------------------------------------------------------------
138 C. Implement the IObjectWithSite interface. 
141 The BHOIEContextMenu.cs file defines the BHO. 
143 The class also implements the IObjectWithSite interface. In the SetSite method, you 
144 can get an instance implemented the InternetExplorer interface.
146      Public Sub SetSite(ByVal site As Object) Implements IObjectWithSite.SetSite
147         If site IsNot Nothing Then
148             ieInstance = CType(site, InternetExplorer)
150             ' Register the DocumentComplete event.
151             AddHandler ieInstance.DocumentComplete, AddressOf ieInstance_DocumentComplete
152         End If
153     End Sub
155     Public Sub GetSite(ByRef guid_Renamed As Guid,
156                        <System.Runtime.InteropServices.Out()> ByRef ppvSite As Object) _
157                    Implements IObjectWithSite.GetSite
158         Dim punk As IntPtr = Marshal.GetIUnknownForObject(ieInstance)
159         ppvSite = New Object()
160         Dim ppvSiteIntPtr As IntPtr = Marshal.GetIUnknownForObject(ppvSite)
161         Dim hr As Integer = Marshal.QueryInterface(punk, guid_Renamed, ppvSiteIntPtr)
162         Marshal.ThrowExceptionForHR(hr)
163         Marshal.Release(ppvSiteIntPtr)
164         Marshal.Release(punk)
165     End Sub
168 -----------
169 Register the BHO:
171 The registration and unregistration logic of the BHO are also implemented in 
172 this class. To register a BHO, a new key should be created under the key:
174 HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects
177 -----------------------------------------------------------------------------
179 D. Deploying the BHO with a setup project.
181   (1) In BHOIEContextMenu, add an Installer class (named BHOInstaller in this 
182    code sample) to define the custom actions in the setup. The class derives from
183    System.Configuration.Install.Installer. We use the custom actions to 
184    register/unregister the COM-visible classes in the current managed assembly
185    when user installs/uninstalls the component. 
187     <RunInstaller(True), ComVisible(False)>
188 Partial Public Class BHOInstaller
189     Inherits Installer
190     Public Sub New()
191         InitializeComponent()
192     End Sub
194     Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
195         MyBase.Install(stateSaver)
197         Dim regsrv As New RegistrationServices()
198         If Not regsrv.RegisterAssembly(Me.GetType().Assembly,
199                                        AssemblyRegistrationFlags.SetCodeBase) Then
200             Throw New InstallException("Failed To Register for COM")
201         End If
202     End Sub
203    
204     Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
205         MyBase.Uninstall(savedState)
206         Dim regsrv As New RegistrationServices()
207         If Not regsrv.UnregisterAssembly(Me.GetType().Assembly) Then
208             Throw New InstallException("Failed To Unregister for COM")
209         End If
210     End Sub
212 End Class
214   In the overriden Install method, we use RegistrationServices.RegisterAssembly 
215   to register the classes in the current managed assembly to enable creation 
216   from COM. The method also invokes the static method marked with 
217   ComRegisterFunctionAttribute to perform the custom COM registration steps. 
218   The call is equivalent to running the command: 
219   
220     "Regasm.exe VBBrowserHelperObject.dll /codebase"
222   in the development environment. 
224   (2) To add a deployment project, on the File menu, point to Add, and then 
225   click New Project. In the Add New Project dialog box, expand the Other 
226   Project Types node, expand the Setup and Deployment Projects, click Visual 
227   Studio Installer, and then click Setup Project. In the Name box, type 
228   VBBrowserHelperObjectSetup(x86). Click OK to create the project. 
229   In the Properties dialog of the setup project, make sure that the 
230   TargetPlatform property is set to x86. This setup project is to be deployed 
231   for 32bit IE on x86 or x64 Windows operating systems. 
233   Right-click the setup project, and choose Add / Project Output ... 
234   In the Add Project Output Group dialog box, VBBrowserHelperObject will  
235   be displayed in the Project list. Select Primary Output and click OK. VS
236   will detect the dependencies of the VBBrowserHelperObject, including .NET
237   Framework 4.0 (Client Profile).       
239   Right-click the setup project again, and choose View / Custom Actions. 
240   In the Custom Actions Editor, right-click the root Custom Actions node. On 
241   the Action menu, click Add Custom Action. In the Select Item in Project 
242   dialog box, double-click the Application Folder. Select Primary output from 
243   VBBrowserHelperObject. This adds the custom actions that we defined 
244   in BHOInstaller of VBBrowserHelperObject. 
246   Build the setup project. If the build succeeds, you will get a .msi file 
247   and a Setup.exe file. You can distribute them to your users to install or 
248   uninstall this BHO. 
250   (3) To deploy the BHO for 64bit IE on a x64 operating system, you 
251   must create a new setup project (e.g. VBBrowserHelperObjectSetup(x64) 
252   in this code sample), and set its TargetPlatform property to x64. 
254   Although the TargetPlatform property is set to x64, the native shim 
255   packaged with the .msi file is still a 32-bit executable. The Visual Studio 
256   embeds the 32-bit version of InstallUtilLib.dll into the Binary table as 
257   InstallUtil. So the custom actions will be run in the 32-bit, which is 
258   unexpected in this code sample. To workaround this issue and ensure that 
259   the custom actions run in the 64-bit mode, you either need to import the 
260   appropriate bitness of InstallUtilLib.dll into the Binary table for the 
261   InstallUtil record or - if you do have or will have 32-bit managed custom 
262   actions add it as a new record in the Binary table and adjust the 
263   CustomAction table to use the 64-bit Binary table record for 64-bit managed 
264   custom actions. This blog article introduces how to do it manually with 
265   Orca http://blogs.msdn.com/b/heaths/archive/2006/02/01/64-bit-managed-custom-actions-with-visual-studio.aspx
267   In this code sample, we automate the modification of InstallUtil by using a 
268   post-build javascript: Fix64bitInstallUtilLib.js. You can find the script 
269   file in the VBBrowserHelperObjectSetup(x64) project folder. To 
270   configure the script to run in the post-build event, you select the 
271   VBBrowserHelperObjectSetup(x64) project in Solution Explorer, and 
272   find the PostBuildEvent property in the Properties window. Specify its 
273   value to be 
274   
275         "$(ProjectDir)Fix64bitInstallUtilLib.js" "$(BuiltOuputPath)" "$(ProjectDir)"
277   Repeat the rest steps in (2) to add the project output, set the custom 
278   actions, configure the prerequisites, and build the setup project.
281 /////////////////////////////////////////////////////////////////////////////
282 Diagnostic:
284 To debug BHO, you can attach to iexplorer.exe. 
288 ///////////////////////////////////////////////////////////////////////////// 
290 References:
292 Browser Helper Objects: The Browser the Way You Want It
293 http://msdn.microsoft.com/en-us/library/ms976373.aspx
295 Hosting and Reuse
296 http://msdn.microsoft.com/en-us/library/aa752038(VS.85).aspx
298 IHTMLDocument2 Interface
299 http://msdn.microsoft.com/en-us/library/aa752574(VS.85).aspx
301 Mouse event handling problem in BHO
302 http://social.msdn.microsoft.com/Forums/en/ieextensiondevelopment/thread/1ea154a5-5802-460c-9941-30f14b36d0a4
304 /////////////////////////////////////////////////////////////////////////////